package com.mywork.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

import com.ciko.code.base.BaseActicity;
import com.ciko.code.bean.WorkMeetingListBean;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.HttpClient;
import com.ciko.code.http.Req.GetWorkMeetingListReq;
import com.ciko.code.utils.NetWorkUtils;
import com.ciko.code.utils.Util;
import com.ciko.code.views.pullrefresh.PullToRefreshBase;
import com.ciko.code.views.pullrefresh.PullToRefreshScrollView;
import com.multi_image_selector.view.MyListView;
import com.mywork.R;
import com.mywork.adapter.MeetingsAdapter;
import com.zxing.qr_codescan.MipcaActivityCapture;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by 12154 on 2015/11/5.
 */
public class WorkMeetingActivity
        extends
        BaseActicity
{
	
	private String[]				  mTitles		 = {
	                                                     "我的会议",
	                                                     "近期会议",
	                                                     "签到记录"
													   };
	private MeetingsAdapter			  adapter1, adapter2;
	private long					  lastSerialCode = 0;				 //最后一次序列号
	private int						  PAGESIZE		 = 1000;				 //每页长度
	private List<WorkMeetingListBean> list1			 = new ArrayList<>();
	private List<WorkMeetingListBean> list2			 = new ArrayList<>();
	private Context					  context;
	private PullToRefreshScrollView	  sV;
	private RelativeLayout			  rlNoneTip;
	private boolean					  isRefresh		 = true;
	private List<WorkMeetingListBean> list			 = new ArrayList<>();
	//    private TabPagerAdapter tabPagerAdapter;
	//
	//    private List<Fragment> fragmentList = new ArrayList<>();
	//
	//    private SwingLableBarWeight slb_meetingWork;
	//    private ViewPager vp_contentMeetingWork;
	private MyListView				  lv1, lv2;							 //lv1最近会议  lv2以往会议
	private LinearLayout              llNew, llOld;
	private ScrollView                scrollView;
	private Handler                   handler = new Handler();
	private String title                      = "";
	@Override
	protected void initTopBar() {
		if (getIntent().getExtras().containsKey("title")) {
			title = getIntent().getStringExtra("title");
		}
		setTitle(title);
		enableNavLeftImg();
//		enableNavRightImg(R.drawable.icon_zxing);
	}
	
	@Override
	protected void initLayout() {
		setContentView(R.layout.activity_work_meeting);
		context = this;
	}
	
	@Override
	protected void initView() {
		//        slb_meetingWork = findViewByID(R.id.slb_meetingWork);
		//        vp_contentMeetingWork = findViewByID(R.id.vp_contentMeetingWork);
		sV = (PullToRefreshScrollView) findViewById(R.id.sv);
		sV.setPullLoadEnabled(true);
		sV.setScrollLoadEnabled(true);
		lv1 = (MyListView) findViewById(R.id.lv_latest_meetings);
		lv2 = (MyListView) findViewById(R.id.lv_old_meetings);
		rlNoneTip = findViewByID(R.id.rl_none_tip_logo);
		llNew = findViewByID(R.id.ll_new);
		llOld = findViewByID(R.id.ll_old);
		llOld.setVisibility(View.GONE);
		llNew.setVisibility(View.GONE);
		scrollView = findViewByID(R.id.scrollView);
	}
	
	@Override
	protected void initAdapter() {
		
		//        fragmentList.add(new WorkMeetingMyFragment());
		//        fragmentList.add(new WorkMeetingLaterFragment());
		//        fragmentList.add(new WorkMeetingRecordFragment());
		//
		//        tabPagerAdapter = new TabPagerAdapter(getSupportFragmentManager(), fragmentList, mTitles);
		//        vp_contentMeetingWork.setAdapter(tabPagerAdapter);
		//        slb_meetingWork.setViewPager(vp_contentMeetingWork);
		adapter1 = new MeetingsAdapter(context, list1, 1);//1最新会议
		adapter2 = new MeetingsAdapter(context, list2, 2);//2以往会议
		lv1.setAdapter(adapter1);
		lv2.setAdapter(adapter2);
		
	}
	
	@Override
	protected void initDates() {
		//FIXME userNo
		//        HttpClient.getInstance(this).post(new SelMeetingListReq("1", WorkUIKit.getUser().getUserNo()), this);
		//        HttpClient.getInstance(this).post(new SelMeetingListReq("2", WorkUIKit.getUser().getUserNo()), this);
		HttpClient.getInstance(this).post(new GetWorkMeetingListReq(lastSerialCode, PAGESIZE), this);
	}
	
	@Override
	protected void initListener() {
		
		sV.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>()
		{
			@Override
			public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
				refresh();
			}
			
			@Override
			public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
				loadMore();
			}
		});
		lv1.setOnItemClickListener(new AdapterView.OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
				Intent intent1 = new Intent(context, WorkMeetingDetailActivity.class);
				intent1.putExtra("meetingId", list1.get(i).meetingId);
				startActivity(intent1);
			}
		});
		
		lv2.setOnItemClickListener(new AdapterView.OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
				Intent intent2 = new Intent(context, WorkMeetingDetailActivity.class);
				intent2.putExtra("meetingId", list2.get(i).meetingId);
				startActivity(intent2);
			}
		});
		setOnNavRightClickListener(new OnNavRightClickListener() {
			@Override
			public void onNavRightClick() {
				Intent intent = new Intent(context, MipcaActivityCapture.class);
				startActivity(intent);
			}
		});
	}
	
	private void refresh() {
		isRefresh = true;
		lastSerialCode = 0;
		HttpClient.getInstance(this).post(new GetWorkMeetingListReq(lastSerialCode, PAGESIZE), this);
		
	}
	
	private void loadMore() {
		isRefresh = false;
		HttpClient.getInstance(this).post(new GetWorkMeetingListReq(lastSerialCode, PAGESIZE), this);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
	@Override
	public void onResponseSuccess(BaseResponse<?> response, String tag) {
		super.onResponseSuccess(response, tag);
		if (isRefresh)
		{
			list = (List<WorkMeetingListBean>) response.getReturnObject();
		}
		else
		{
			list.addAll((List<WorkMeetingListBean>) response.getReturnObject());
		}
		if (list.size() == 0)
		{
			rlNoneTip.setVisibility(View.VISIBLE);
			return;
		}
		
		sV.onPullDownRefreshComplete();
		sV.onPullUpRefreshComplete();
		for (int i = 0; i < list.size(); i++)
		{
			if (list.get(i).type == 0)
			{
				list2.add(list.get(i));
			}
			else
			{
				list1.add(list.get(i));
			}
		}
		
		if (list1.size() == 0)
		{
			llNew.setVisibility(View.GONE);
		}
		else {
			llNew.setVisibility(View.VISIBLE);
		}
		if (list2.size() == 0)
		{
			llOld.setVisibility(View.GONE);
		}
		else {
			llOld.setVisibility(View.VISIBLE);
		}
		adapter1.notifyDataSetChanged();
		adapter2.notifyDataSetChanged();
		handler.post(new Runnable() {
			@Override
			public void run() {
				scrollView.scrollTo(0, 0);
			}
		});
		lastSerialCode = list.get(list.size() - 1).serialCode;
	}
	
	@Override
	public void onResponseFail(String tag, int resultCode) {
		super.onResponseFail(tag, resultCode);
		if (NetWorkUtils.isConnected(context))
		                                       Util.showToast(context, "加载失败，请重试");
	}
}
